package ext.acae2.change.helper;


import java.util.List;

import org.apache.log4j.Logger;

import wt.doc.WTDocument;
import wt.enterprise.RevisionControlled;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.part.WTPartConfigSpec;
import wt.part.WTPartStandardConfigSpec;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.VersionControlHelper;
import wt.vc.views.ViewHelper;

import com.ptc.netmarkets.model.NmOid;

import ext.acae2.part.PartHelper;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-5-14 下午11:36:21
 */
public class ChangeTaskHelper {
	
	private static final  Logger LOGGER =  LogR.getLogger(ChangeTaskHelper.class.getName());
	
	/**
	 * 
	 * @description 
	 *   check file ECN resulting item is the primary Object revise Object
	 * @author cnie
	 * @date 2013-5-14 下午10:34:44
	 * @version 1.0
	 * @param primaryDoc
	 * @param resultItemOid
	 * @return
	 * @throws WTException
	 */
	public static boolean checkFileECNResultItem(WTDocument primaryDoc,List<NmOid> resultItemList) throws WTException{
		boolean isPrimaryResultItem = false;
		NmOid nmoid = resultItemList.get(0);
		if(nmoid.isA(WTDocument.class)){
			WTDocument resultDoc = (WTDocument)nmoid.getWtRef().getObject();
			isPrimaryResultItem = checkObjRevise(primaryDoc,resultDoc);
		}
		return isPrimaryResultItem;
	}
	
	/**
	 * 
	 * @description 
	 *   check part ECN resulting Item is the primary part releated Object
	 * @author cnie
	 * @date 2013-5-14 下午10:53:15
	 * @version 1.0
	 * @param primaryPart
	 * @param resultItemOids
	 * @return
	 */
	public static boolean checkPartECNResultItem(List<NmOid> resultItemList,List<RevisionControlled> releatedItems){
		
		boolean isPrimaryRelatedItem = false;
		
		try {
			for(NmOid nmoid : resultItemList){
				isPrimaryRelatedItem =false;
				RevisionControlled resultRc = (RevisionControlled)nmoid.getWtRef().getObject();
				for(RevisionControlled rc:releatedItems){
					isPrimaryRelatedItem = isPrimaryRelatedItem||checkObjRevise(rc, resultRc);
				}
				if(!isPrimaryRelatedItem){
					break;
				}
			}
		} catch (WTException e) {
			e.printStackTrace();
		}
		
		return isPrimaryRelatedItem;
		
	}
	
	/**
	 * 
	 * @description
	 *   check obj is revise 
	 * @author cnie
	 * @date 2013-5-14 下午11:22:08
	 * @version 1.0
	 * @param rc
	 * @param newVersionRc
	 * @return
	 * @throws WTException
	 * @throws WTPropertyVetoException
	 */
	public static boolean checkObjRevise(RevisionControlled rc,RevisionControlled newVersionRc) throws WTException{
		
		boolean isNewVersion = false;
		if(rc.getMaster().equals(newVersionRc.getMaster())){
			try {
				String newVersion = newVersionRc.getVersionInfo().getIdentifier().getValue();
				String nextVersion = VersionControlHelper.nextVersionId(rc).getValue();
				if(newVersion.equals(nextVersion)&&rc.getMaster().equals(newVersionRc.getMaster())){
					isNewVersion =true;
				}
			} catch (WTPropertyVetoException e) {
				e.printStackTrace();
			}
			
		}
		return isNewVersion;
	}
	
	/**
	 * 得到part结构树关联的cad文档的对象（采用最新的视图）
	 * @description TODO
	 * @author cnie
	 * @date 2013-6-9 上午11:51:38
	 * @version 1.0
	 * @param part
	 * @return
	 * @throws WTException
	 */
	public static List<RevisionControlled> getPrimaryRelectedCADDocs(WTPart part,List<RevisionControlled> rcs) throws WTException{
		WTPartStandardConfigSpec standardConfigSpec = WTPartStandardConfigSpec
				.newWTPartStandardConfigSpec(
						ViewHelper.service.getView("Design"), null);
		WTPartConfigSpec configSpec = WTPartConfigSpec
				.newWTPartConfigSpec(standardConfigSpec);
		configSpec.setStandardActive(true);
		PartHelper.getBomConstructAssociatedCADDoc(part,configSpec,rcs);
		return rcs;
	}
	

}
